package com.example.demo.advice;


import com.example.demo.exception.ParameterException;
import com.example.demo.pojo.ResBuilder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionHandleAdvice {
    /**
     * 全局异常捕获
     */
    @ExceptionHandler({Throwable.class})
    public Object handleThrowable(Throwable e){
        e.printStackTrace();
        return "服务器开小差了，请稍后再试。";
    }

    /**
     * 参数异常捕获，给前段返回数据
     */
    @ExceptionHandler({ParameterException.class})
    public Object handleParameterException(ParameterException e){
        e.printStackTrace();
        return ResBuilder.getError(e.getCode(),e.getMsg());
    }
}
